#include "Stdafx.h"
#include "SWScrollView.h"
#include "SWViewConst.h"


CSWScrollView::CSWScrollView()
{
	m_bUpdate = FALSE;
	m_hMemDC  = 0;
	m_hBmp    = 0;

	m_PtScroll.x = 0;
	m_PtScroll.y = 0;
}

CSWScrollView::~CSWScrollView()
{
	SAFE_DELETE_OBJECT(m_hBmp);
	SAFE_DELETE_DC(m_hMemDC);
}


//
void CSWScrollView::OnBasePaint(HDC hDC)
{
	if( !hDC )
	{
		ASSERT(FALSE);
		return ;
	}

	HBITMAP hOldBitmap = NULL;
	if( m_bUpdate)
	{
		m_bUpdate = FALSE;
		//
		SAFE_DELETE_OBJECT(m_hBmp);
		SAFE_DELETE_DC(m_hMemDC);
		//
		m_hMemDC = ::CreateCompatibleDC(hDC);
		//
		m_hBmp = ::CreateCompatibleBitmap( hDC,m_Rc.Width(),m_Rc.Height());

	}

	//
	hOldBitmap = (HBITMAP)::SelectObject(m_hMemDC,m_hBmp);
	OnDrawView(m_hMemDC);

	//
	::BitBlt( hDC,m_Rc.left,m_Rc.top,m_Rc.Width(),m_Rc.Height(),
		m_hMemDC,m_PtScroll.x,m_PtScroll.y,SRCCOPY );

	if( hOldBitmap ) 
		::SelectObject( m_hMemDC,hOldBitmap ) ;
}

void CSWScrollView::OnDrawView(HDC hMemDC)
{
}

void CSWScrollView::Update(/*= NULL*/)
{
	m_bUpdate = TRUE;

}

void CSWScrollView::OnScrollChange(const POINT& point)
{
	m_PtScroll = point;
}

void CSWScrollView::OnScrollChange(const int x,const int y)
{
	m_PtScroll.x = x;
	m_PtScroll.y = y;

	//
	m_bUpdate = TRUE;
}

void CSWScrollView::ClientToDC(LPPOINT lpPoint)
{
	if( !lpPoint )
		return ;
	//
	lpPoint->x = m_PtScroll.x + lpPoint->x;
	lpPoint->y = m_PtScroll.y + lpPoint->y;
}

void CSWScrollView::SetCanvasSize(SIZE size)
{
	m_Rc.left  = 0;
	m_Rc.top   = 0;
	m_Rc.right = m_Rc.left + size.cx ;
	m_Rc.bottom = m_Rc.top + size.cy ;
}

void CSWScrollView::SetCanvasSize(const int x,const int y)
{
	m_Rc.left  = 0;
	m_Rc.top   = 0;
	m_Rc.right = m_Rc.left + x ;
	m_Rc.bottom = m_Rc.top + y ;
}


void CSWScrollView::ConvertClientToDC(CPoint& Pt)
{
	POINT PtScroll = GetScrollPoint();
	Pt.x = PtScroll.x + Pt.x;
	Pt.y = PtScroll.y + Pt.y;
}